﻿package  {
	
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.events.MouseEvent;
	import flash.text.TextFormat;
	
	
	public class MissionSelect extends MovieClip {
		
		var missions:Vector.<misButton>;
		var misTitle:TextField;
		var format:TextFormat;
		
		
		public function MissionSelect() {
			
			missions = new Vector.<misButton>();
			misTitle = new TextField();
			addChild(misTitle);
			misTitle.x = 15; misTitle.y = 440;
			misTitle.width = 120; misTitle.height = 35; 
			misTitle.textColor = 0xff000;
			misTitle.thickness = 20;
			misTitle.visible = false;
			format = new TextFormat();
			format.size = 20;
			misTitle.defaultTextFormat = format;
			
			
			missions.push(mis0);
			mis0.missionTitle = "Graveyard";
			mis0.gotoAndStop("Locked");
			mis0.defaultState = "Locked";
			mis0.addEventListener(MouseEvent.CLICK, select0);
			
			missions.push(mis1);
			mis1.missionTitle = "Crossroads";
			mis1.gotoAndStop("Locked");
			mis1.defaultState = "Locked";
			mis1.addEventListener(MouseEvent.CLICK, select1);
			
			missions.push(mis2);
			mis2.missionTitle = "Volcano Cave";
			mis2.gotoAndStop("Locked");
			mis2.defaultState = "Locked";
			mis2.addEventListener(MouseEvent.CLICK, select2);
			
			missions.push(mis3);
			mis3.missionTitle = "Temple";
			mis3.gotoAndStop("Locked");
			mis3.defaultState = "Locked";
			mis3.addEventListener(MouseEvent.CLICK, select3);
			
			missions.push(mis4);
			mis4.missionTitle = "Finale";
			mis4.gotoAndStop("Locked");
			mis4.defaultState = "Locked";
			mis4.addEventListener(MouseEvent.CLICK, select4);
		}
		
		public function missionComplete(mis:int)
		{
			missions[mis].gotoAndStop("Completed");
			missions[mis].defaultState = "Completed"; 
		}
		
		public function unlock(mis:int)
		{
			missions[mis].gotoAndPlay("Idle");
			missions[mis].defaultState = "Idle";
		}
		
		private function select(mis:misButton):void
		{
			if(mis.currentFrameLabel != "Locked")
			{
				for(var i:int = 0; i < missions.length; i++)
				{
					missions[i].gotoAndStop(missions[i].defaultState);
					if(missions[i].defaultState == "Idle")
					{
						missions[i].play();
					}
				}
				mis.gotoAndStop("Select");
				misTitle.text = mis.missionTitle;
				misTitle.visible = true;
			}
		}
		
		private function select0(e:MouseEvent):void
		{
			select(missions[0]);
		}
		
		private function select1(e:MouseEvent):void
		{
			select(missions[1]);
		}
		
		private function select2(e:MouseEvent):void
		{
			select(missions[2]);
		}
		
		private function select3(e:MouseEvent):void
		{
			select(missions[3]);
		}
		
		private function select4(e:MouseEvent):void
		{
			select(missions[4]);
		}
		
		
		public function cleanup():void
		{
			for(var i:int = 0; i < missions.length; i++)
			{
				missions[i].gotoAndStop("Locked");
				misTitle.visible = false;
			}
		}
	}
	
}
